package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.entity.Category;
import com.situ.mall.entity.Result;
import com.situ.mall.mapper.CategoryMapper;
import com.situ.mall.service.ICategoryService;

@Service
public class CategoryServiceImpl implements ICategoryService{
	@Autowired
	private CategoryMapper categoryMapper;
	
	
// 后台back
// 0.0 //*******product商品的添加功能      获取 category' service&mapper层 获取 一级和二级分类********
	@Override
	public List<Category> getTopCategoryList() {
		System.out.println("CategoryServiceImpl.getTopCategoryList()");
		return categoryMapper.getTopCategoryList();
	}
	@Override
	public List<Category> selectSecondCategoryByTopCategoryId(Integer topCategoryId) {
		System.out.println("CategoryServiceImpl.selectSecondCategoryByTopCategoryId()");
		return categoryMapper.selectSecondCategoryByTopCategoryId(topCategoryId);
	}
	
// 0.测试
	@Override
	public List<Category> selectAll() {
		return categoryMapper.selectAll();
	}

// 1.页面展示 
	@Override
	public Result selectByPage(Integer page, Integer limit) {
		//总个数
		int count = categoryMapper.getTotalCount();
		
		//每页信息
		int offset = (page-1)*limit;
		List<Category> list = categoryMapper.selectByPage(offset,limit);
		
		return Result.success("", list, count);
	}

//2.批量删除
	@Override
	public boolean deleteAll(Integer[] ids) {
		int count = 0;				//1.记数
		for (Integer id : ids) {	//2.循环删除，根据id.length 有几个id删除几次
			int c = categoryMapper.deleteByPrimaryKey(id); //3. 每次 根据id删除
			count+=c;	//4.删除一次，删除记录+1
		}
		return count== ids.length ? true:false; // 5.删除记录次数 == id[]个数相等 (确保都删了)；删除成功，否则失败05
	}

//3.删除
	@Override
	public boolean deleteById(Integer id) {
		int count= categoryMapper.deleteByPrimaryKey(id);
		return count==1 ? true:false;
	}
	
//4.1.添加已积分类
	@Override
	public Result insertTopCategory(Category category) {
		System.out.println("CategoryServiceImpl.insertTopCategory()");
		int count = categoryMapper.insert(category);
		return count==1? Result.success("添加成功") : Result.error("添加失败");
	}
	
//echarts 统计数量
	@Override
	public List<Category> getCategoryCount() {
		System.out.println("CategoryServiceImpl.getCategoryCount()");
		
		List<Category> list = categoryMapper.getCategoryCount();
		return list;
	}
	
//前台front
	//1.获取二级分类 （全部）
	@Override
	public List<Category> getSecondCategoryList() {
		System.out.println("CategoryServiceImpl.getSecondCategoryList()");
		return categoryMapper.getSecondCategoryList();
	}



}
